package com.meilai.project.controller.web.administration.seal;


import com.meilai.project.dto.CommonResult;
import com.meilai.project.dto.administration.seal.BorrowSealFlowDTO;
import com.meilai.project.dto.workflow.WorkFlowExtraCheckDTO;
import com.meilai.project.entity.administration.seal.BorrowSealFlow;
import com.meilai.project.service.administration.seal.BorrowSealFlowService;
import com.meilai.project.vo.administration.seal.DictSealTypeVO;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

/**
 * <p>
 * 借用印章申请表 前端控制器
 * </p>
 *
 * @author xhy
 * @since 2022-01-24
 */
@Api(value = "借用印章控制器", tags = {"借用印章"})
@RestController
@RequestMapping("api/web/borrow_seal_flow")
public class BorrowSealFlowController {

    @Autowired
    private BorrowSealFlowService service;


    @ApiOperation(value = "列表分页查询", notes = "查看申请记录", response = BorrowSealFlow.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("list")
    public CommonResult list(@ApiParam(name = "page", value = "当前页", required = true)
                             @RequestParam(value = "page", defaultValue = "1", required = true) Integer page,
                             @ApiParam(name = "page_size", value = "每页数量", required = true)
                             @RequestParam(value = "page_size", defaultValue = "10", required = true) Integer page_size,
                             @ApiParam(name = "is_manage_page", value = "是否是管理界面")
                             @RequestParam(name = "is_manage_page", required = false) Boolean is_manage_page,
                             @ApiParam(name = "name", value = "人员名称")
                             @RequestParam(name = "name", required = false) String name,
                             @ApiParam(name = "seal_type_id", value = "印章类型ID")
                             @RequestParam(name = "seal_type_id", required = false) Long seal_type_id,
                             @ApiParam(name = "seal_type_name", value = "印章类型名称")
                             @RequestParam(name = "seal_type_name", required = false) String seal_type_name,
                             @ApiParam(name = "borrowing_time_start", value = "借用开始时间")
                             @RequestParam(name = "borrowing_time_start", required = false) String borrowing_time_start,
                             @ApiParam(name = "borrowing_time_end", value = "借用结束时间")
                             @RequestParam(name = "borrowing_time_end", required = false) String borrowing_time_end,
                             @ApiParam(name = "estimated_return_time_start", value = "预计归还开始时间")
                             @RequestParam(name = "estimated_return_time_start", required = false) String estimated_return_time_start,
                             @ApiParam(name = "estimated_return_time_end", value = "预计归还结束时间")
                             @RequestParam(name = "estimated_return_time_end", required = false) String estimated_return_time_end,
                             @ApiParam(name = "return_time_start", value = "实际归还开始时间")
                             @RequestParam(name = "return_time_start", required = false) String return_time_start,
                             @ApiParam(name = "return_time_end", value = "实际归还结束时间")
                             @RequestParam(name = "return_time_end", required = false) String return_time_end,
                             @ApiParam(name = "company_id", value = "公司ID")
                             @RequestParam(name = "company_id", required = false) Long company_id) {
        return new CommonResult().success(service.selectPage(page, page_size, is_manage_page, name, seal_type_id, seal_type_name, borrowing_time_start, borrowing_time_end, estimated_return_time_start, estimated_return_time_end, return_time_start, return_time_end,company_id));
    }

    @ApiOperation(value = "根据ID获取详情", notes = "根据ID获取详情", response = BorrowSealFlow.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("one")
    public CommonResult one(@ApiParam(name = "id", required = true) @RequestParam(name = "id") Long id) {
        return new CommonResult().success(service.one(id));
    }

    @ApiOperation(value = "保存", notes = "保存")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("save")
    public CommonResult save(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) BorrowSealFlowDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.save(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "提交", notes = "提交")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PostMapping("begin")
    public CommonResult begin(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) BorrowSealFlowDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.begin(dto)) {
                return new CommonResult().success("新增成功");
            } else {
                return new CommonResult().failed("新增失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "通过", notes = "通过")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("pass")
    public CommonResult pass(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) WorkFlowExtraCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.pass(dto)) {
                return new CommonResult().success("通过成功");
            } else {
                return new CommonResult().failed("通过失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "驳回", notes = "驳回")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("return_back")
    public CommonResult returnBack(@RequestBody @Valid @ApiParam(name = "传输对象", value = "传入json格式", required = true) WorkFlowExtraCheckDTO dto
            , BindingResult bindingResult) {
        if (bindingResult.hasErrors()) return new CommonResult().validateFailed(bindingResult);
        try {
            if (service.returnBack(dto)) {
                return new CommonResult().success("驳回成功");
            } else {
                return new CommonResult().failed("驳回失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "删除", notes = "删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("delete")
    public CommonResult delete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.delete(id)) {
                return new CommonResult().success("删除成功");
            } else {
                return new CommonResult().failed("删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "彻底删除", notes = "彻底删除")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @DeleteMapping("actual_delete")
    public CommonResult actualDelete(@RequestParam(value = "id", required = true) Long id) {
        try {
            if (service.actualDelete(id)) {
                return new CommonResult().success("彻底删除成功");
            } else {
                return new CommonResult().failed("彻底删除失败");
            }
        } catch (Exception e) {
            return new CommonResult().failed(e.getMessage());
        }
    }

    @ApiOperation(value = "获取印章类型数据", notes = "获取印章类型数据", response = DictSealTypeVO.class)
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @GetMapping("get_seal_type_list")
    public CommonResult getSealTypeList(@RequestParam(value = "company_id", required = false) Long company_id) {
        return new CommonResult().success(service.getSealTypeList(company_id));
    }

    @ApiOperation(value = "归还", notes = "归还印章")
    @ApiResponses({@ApiResponse(code = CommonResult.SUCCESS, message = "操作成功"),
            @ApiResponse(code = CommonResult.VALIDATE_FAILED, message = "参数校验失败"),
            @ApiResponse(code = CommonResult.UNAUTHORIZED, message = "未认证"),
            @ApiResponse(code = CommonResult.FORBIDDEN, message = "未授权"),
            @ApiResponse(code = CommonResult.FAILED, message = "操作失败")})
    @ResponseBody
    @PutMapping("seal_return")
    public CommonResult sealReturn(@RequestParam(value = "id", required = true) Long id) {
        return new CommonResult().success(service.sealReturn(id));
    }
}

